@echo off
setlocal enabledelayedexpansion

echo ==================================================
echo           MinFS EasyClient 测试启动脚本
echo ==================================================
echo.

:: 检查Java环境
java -version >nul 2>&1
if %errorlevel% neq 0 (
    echo 错误: 未找到Java环境，请安装Java 8+
    pause
    exit /b 1
)

:: 检查Maven环境
mvn -version >nul 2>&1
if %errorlevel% neq 0 (
    echo 错误: 未找到Maven环境，请安装Maven 3.6+
    pause
    exit /b 1
)

echo 环境检查通过...
echo.

:: 检查服务依赖
echo 检查服务依赖状态...
echo.

:: 检查ZooKeeper
echo 检查 ZooKeeper (localhost:2181)...
netstat -an | find "2181" | find "LISTENING" >nul
if %errorlevel% equ 0 (
    echo ✓ ZooKeeper 服务运行中
) else (
    echo ✗ ZooKeeper 服务未运行
    echo   请先启动 ZooKeeper 服务
)

:: 检查MetaServer
echo 检查 MetaServer (localhost:8300)...
netstat -an | find "8300" | find "LISTENING" >nul
if %errorlevel% equ 0 (
    echo ✓ MetaServer 服务运行中
) else (
    echo ✗ MetaServer 服务未运行
    echo   请先启动 MetaServer 服务
)

:: 检查DataServer
echo 检查 DataServer (localhost:8400+)...
netstat -an | find "8400" | find "LISTENING" >nul
if %errorlevel% equ 0 (
    echo ✓ DataServer 服务运行中
) else (
    echo ○ DataServer 状态未知（端口号可能不同）
)

echo.
echo 按任意键继续运行测试，或 Ctrl+C 取消...
pause >nul

echo.
echo --------------------------------------------------
echo 编译客户端代码...
echo --------------------------------------------------

:: 编译项目
call mvn clean compile -q
if %errorlevel% neq 0 (
    echo 编译失败，请检查代码
    pause
    exit /b 1
)

echo 编译成功!
echo.

echo --------------------------------------------------
echo 运行客户端测试...
echo --------------------------------------------------
echo.

:: 运行测试
call mvn exec:java -Dexec.mainClass="com.ksyun.campus.client.ClientTest" -q

echo.
echo --------------------------------------------------
echo 测试完成
echo --------------------------------------------------

if %errorlevel% equ 0 (
    echo ✓ 客户端测试运行完成
) else (
    echo ✗ 客户端测试运行失败
    echo 请查看错误信息并检查服务状态
)

echo.
echo 按任意键退出...
pause >nul
